import functools
from flask import current_app
from flask import g
from flask import session

from info.models import User


def to_index_class(index):
    if index==1:
        return 'first'
    elif index==2:
        return 'second'
    elif index==3:
        return 'third'
    else:
        return ''


def login_user_data(view_func):
    @functools.wraps(view_func)
    def wrapper(*args,**kwargs):
        # 1. 从session获取用户id
        user_id = session.get('user_id', None)

        # 2. 根据用户id查询数据
        user = None
        if user_id:
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
        g.user=user
        return view_func(*args,**kwargs)

    return wrapper